package com.ttbj.rule.service.impl;

import org.springframework.stereotype.Component;
import com.ttbj.product.bean.dto.CargoDto;
import com.ttbj.rule.po.CarriageRule;
import com.ttbj.rule.service.BillService;
import com.ttbj.rule.service.context.BillContext;

@Component("billService")
public class BillServiceAlpha implements BillService {
	/* 计费类型 0：重量计费 1：体积计费 2：优先计费 */
	private static final int WEIGHT = 0;
	private static final int BULK = 1;
	private static final int NUM = 2;
	private static final int DIS = 3;
	private static final int BOTH = 4;
	private BillContext billContext = null;
	private CargoDto cargo;
	private CarriageRule bill = null;
	private Float distance;

	public BillServiceAlpha(Float distance, CarriageRule bill, CargoDto cargoDto) {
		this.bill = bill;
		this.cargo = cargoDto;
		this.distance = distance;
	}

	public BillServiceAlpha() {
	}

	@Override
	public Float getCarriage() throws Exception {
		/* 根据公司编号查询bill计费:规则每家公司只有一种计费规则 */
		Float carriage = null;
		Integer billType = bill.getBillType();
		switch (billType) {
		case WEIGHT: // 按重量计算费用
			billContext = new BillContext(new BillWeightStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case BULK: // 按体积计算费用
			billContext = new BillContext(new BillBulkStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case NUM: // 数量计费
			billContext = new BillContext(new BillNumStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case DIS://按距离里程阶梯式计费   （（正式计费方式））
			billContext = new BillContext(new BillDistanceStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case BOTH: // 优先计费 重量和体积
			billContext = new BillContext(new BillWeightStrategy());
			Float weight = billContext.contextInterface(distance,cargo, bill);
			billContext = new BillContext(new BillBulkStrategy());
			Float bulk = billContext.contextInterface(distance,cargo, bill);
			carriage = (weight >= bulk) ? weight : bulk;
			break;
		default:
			carriage = 0f; // 剩余计费
			break;
		}
		return null == carriage ? 0f:carriage;
	}

	@Override
	public Float call() throws Exception {
		return getCarriage();
	}

	@Override
	public Float getCarriage(Float distance, CarriageRule bill, CargoDto cargoDto) throws Exception {
		/* 根据公司编号查询bill计费:规则每家公司只有一种计费规则 */
		Float carriage = null;
		Integer billType = bill.getBillType();
		switch (billType) {
		case WEIGHT: // 按重量计算费用
			billContext = new BillContext(new BillWeightStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case BULK: // 按体积计算费用
			billContext = new BillContext(new BillBulkStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case NUM: // 数量计费
			billContext = new BillContext(new BillNumStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case DIS://按距离
			billContext = new BillContext(new BillDistanceStrategy());
			carriage = billContext.contextInterface(distance,cargo, bill);
			break;
		case BOTH: // 优先计费 重量和体积
			billContext = new BillContext(new BillWeightStrategy());
			Float weight = billContext.contextInterface(distance,cargo, bill);
			billContext = new BillContext(new BillBulkStrategy());
			Float bulk = billContext.contextInterface(distance,cargo, bill);
			carriage = (weight >= bulk) ? weight : bulk;
			break;
		default:
			carriage = 0f; // 剩余计费
			break;
		}
		return carriage;
	}

}
